package cn.com.gridinfo.sooc.core.course.discuss.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.discuss.activity.DiscussInfoActivity;
import cn.com.gridinfo.sooc.core.course.discuss.bean.Discuss;
import cn.com.gridinfo.sooc.core.course.discuss.bean.DiscussHfnr;
import cn.com.gridinfo.sooc.utils.DateUtil;
import cn.com.gridinfo.sooc.utils.widget.textview.HtmlImageGetterUtil;

/**
 * 课程-讨论详情 adapter
 * Created by yingchunli on 2015/12/31.
 */
public class DiscussInfoAdapter extends UltimateViewAdapter<DiscussInfoAdapter.ViewHolder> {

    private List<DiscussHfnr> discusseList;
    private Context context;

    public DiscussInfoAdapter(List<DiscussHfnr> discusseList,Context context) {
        this.discusseList = discusseList;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.course_discuss_info_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= discusseList.size() : position < discusseList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            holder.from_name.setText(discusseList.get(position).getFrom_name());

            holder.hfnr.setText("回复" +  discusseList.get(position).getTo_name() + ": " +  discusseList.get(position).getHfnr());

            holder.time.setText(discusseList.get(position).getTime_show());
            holder.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemClickLitener.OnItemClick(v, position);
                }
            });
            //分割线的显示
            if ((discusseList.size() - 1) == position) {
                holder.line.setVisibility(View.VISIBLE);
            } else {
                holder.line.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {
        void OnItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public void loadmore(List<DiscussHfnr> list) {
        discusseList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<DiscussHfnr> list) {
        discusseList.clear();
        discusseList.addAll(list);
        notifyDataSetChanged();
    }

    public DiscussHfnr getObject(int position) {
        return discusseList.get(position);
    }


    @Override
    public int getAdapterItemCount() {
        return discusseList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder {
        TextView time;//时间
        TextView hfnr;//讨论内容
        TextView from_name;//回复人
        LinearLayout layout;
        View line;

        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                layout = (LinearLayout) itemView.findViewById(R.id.discuss_info_item);
                from_name = (TextView) itemView.findViewById(R.id.hfr_discuss_info_item);
                time = (TextView) itemView.findViewById(R.id.time_discuss_info_item);
                hfnr = (TextView) itemView.findViewById(R.id.tl_info);
                line = itemView.findViewById(R.id.line);
            }
        }


    }
}